import AppSettings from "@runtime/base/AppSettings";
import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";
import AssetLoaderComponent from "@runtime/UnityEngineBridge/AssetLoaderComponent";
import { Ref, ref, WatchStopHandle } from "@vue/runtime-dom";
import { message } from 'ant-design-vue';
import MessageCode from "./MessageCode";
// import OrbitCameraComponent from "./OrbitCameraComponent";

/**
 * 起始页
 */
export default class HomeContent {

    public styleData: Ref<any> = ref([])
    public pointData: Ref<any> = ref([])
    public detailData: Ref<any> = ref({})

    private static instance: HomeContent = new HomeContent();
    /**
     * 起始页
     */
    public static get Default(): HomeContent {
        return this.instance;
    }
 
    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);

        for (const watchStopHandle of this.watchStopHandles) {
            watchStopHandle();
        }
        this.watchStopHandles.clear();
    }


    public Loaded: Ref<boolean> = ref(false);

    private watchStopHandles: Array<WatchStopHandle> = [];

    public ProgressText: Ref<string> = ref('');
    public ProgressVal: Ref<number> = ref(0);

    //数据监听
    public OnReceive(id: number, content: string): void {
        console.log(`${id} - ${content}`);
        if (this.IsJsonString(content)) {
            console.log(JSON.parse(content));
        }
    }
    //判断类型
    IsJsonString(str: any) {
        try {
            JSON.parse(str);
        } catch (e) {
            return false;
        }
        return true;
    }

    // public async Awake(): Promise<void> {
    //     this.Loaded.value = true;


    //     // 启动Unity
    //     // 资源编号R00052
    //     const configuration = JSON.stringify({
    //         LocalAddress: AppSettings.Default.LocalAddress,
    //         ShortName: 'P10072',
    //         Type: 12,
    //     });

    //     await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.EmbeddedApplicationLaunchRequest('../EmbeddedApplication/Unity/Mix.Unity.exe', configuration));


    //     // // 获取Unity版本状态
    //     await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.EmbeddedApplicationVersionRequest());


    //     let newArr: any = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.DiagnosisAdapterRequest(MessageCode.Default.MsgCodesList.SYSTEM_GET_ASSET))
    //     let AssetList: any[] = []
    //     JSON.parse(newArr.Content).Result.forEach((item: any) => {
    //         AssetList.push(item.Asset)
    //     });
        
    //     await AssetLoaderComponent.Default.LoadAllAsync(AssetList);

    //     let viewArr: any = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.DiagnosisAdapterRequest(MessageCode.Default.MsgCodesList.VIEW_DATA_CONFIG, '1'))
    //     let ValData = JSON.parse(viewArr.Content).Result
    //     // await OrbitCameraComponent.Default.UpdateV2Async(ValData.TargetContainer, ValData.Horizontal, ValData.Vertical, ValData.Distance, ValData.MinDistance, ValData.MaxDistance, ValData.VerticalAngleMax, ValData.VerticalAngleMin, 1, 1, 1, ValData.HorizontalAngleMax, ValData.HorizontalAngleMin);
    //     // await OrbitCameraComponent.Default.UpdateV2Async('汽车维修4S店/整车模块', 1, 45, 2.0, 0.3, 3.0, 1, 1, 1);
    // }

    // public async Destroy(): Promise<void> {
    // }
}